#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    vector<int> luckyNumbers(vector<vector<int>> &matrix)
    {
        int m = matrix.size(), n = matrix[0].size();
        vector<int> ret;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                bool isMin = true, isMax = true;
                for (int k = 0; k < n; k++)
                {
                    if (matrix[i][k] < matrix[i][j])
                    {
                        isMin = false;
                        break;
                    }
                }
                if (!isMin)
                {
                    continue;
                }
                for (int k = 0; k < m; k++)
                {
                    if (matrix[k][j] > matrix[i][j])
                    {
                        isMax = false;
                        break;
                    }
                }
                if (isMax)
                {
                    ret.push_back(matrix[i][j]);
                }
            }
        }
        return ret;
    }

    vector<int> luckyNumbers_2(vector<vector<int>> &matrix)
    {
        int row = matrix.size(), col = matrix[0].size(); //获取列数
        vector<int> maxCol(col), minRow(row, 100000);
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                minRow[i] = min(minRow[i], matrix[i][j]);
                maxCol[j] = max(maxCol[j], matrix[i][j]);
            }
        }
        vector<int> ret;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                if (matrix[i][j] == minRow[i] && matrix[i][j] == maxCol[j])
                {
                    ret.push_back(matrix[i][j]);
                }
            }
        }
        return ret;
    }
};
int main()
{

    system("pause");
    return 0;
}
